package com.hdoit.ares.web.acc.wechat;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.binarywang.wxpay.bean.request.WxEntPayRequest;
import com.github.binarywang.wxpay.bean.request.WxPayBaseRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.result.WxEntPayResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.hdoit.ares.logical.acc.model.AccountDetail;
import com.hdoit.ares.logical.acc.model.AccountInfo;
import com.hdoit.ares.logical.acc.model.GoldenBeanDetail;
import com.hdoit.ares.logical.acc.model.GoldenBeanInfo;
import com.hdoit.ares.logical.acc.model.IncomeDetail;
import com.hdoit.ares.logical.acc.model.IncomeInfo;
import com.hdoit.ares.logical.acc.model.WithdrawParam;
import com.hdoit.ares.logical.acc.service.IAccountDetailService;
import com.hdoit.ares.logical.acc.service.IAccountInfoService;
import com.hdoit.ares.logical.acc.service.IAccountRechargeService;
import com.hdoit.ares.logical.acc.service.IGoldenBeanDetailService;
import com.hdoit.ares.logical.acc.service.IGoldenBeanInfoService;
import com.hdoit.ares.logical.acc.service.IIncomeDetailService;
import com.hdoit.ares.logical.acc.service.IIncomeInfoService;
import com.hdoit.ares.logical.acc.service.IWithdrawApplyforService;
import com.hdoit.ares.logical.acc.service.IWithdrawParamService;
import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.cust.service.ICustomerService;
import com.hdoit.ares.logical.viewmodel.acc.FindAccountDetail;
import com.hdoit.ares.logical.viewmodel.acc.FindGoldenDetail;
import com.hdoit.ares.logical.viewmodel.acc.FindIncomeDetail;
import com.hdoit.ares.logical.viewmodel.acc.GoldenBeanDetailSearch;
import com.hdoit.ares.logical.viewmodel.acc.Withdraw;
import com.hdoit.ares.selfcommon.enums.AresManageContainer;
import com.tennetcn.common.logical.setting.dao.IParamOptionDao;
import com.tennetcn.common.logical.setting.model.ParamOption;
import com.tennetcn.common.message.WechatLoginModel;
import com.tennetcn.common.utils.DESedeCoder;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.common.utils.JsonUtils;
import com.tennetcn.common.utils.MD5Utils;
import com.tennetcn.common.utils.RandomUtils;
import com.tennetcn.component.wechat.BaseWechat;
import com.tennetcn.web.cbase.wechat.WechatPayService;

@Controller
@RequestMapping(value = "/wechat/acc/myWealth/")
public class MyWealthWechat extends BaseWechat {

	@Override
	public String getRoutePath() {
		return "areas/wechat/acc/myWealth/";
	}

	private IIncomeDetailService incomeDetailService;
	private IIncomeInfoService incomeInfoService2;
	private IGoldenBeanDetailService goldenBeanDetailService;
	private IGoldenBeanInfoService incomeInfoService;
	private IAccountDetailService accountDetailService;
	private IAccountInfoService accountInfoService;
	private IAccountRechargeService accountRechargeService;
	private IWithdrawApplyforService withdrawApplyforService;
	private WechatPayService wechatPayService;
	private IParamOptionDao paramOptionDao;
	private IWithdrawParamService withdrawParamService;

	public IIncomeDetailService getIncomeDetailService() {
		return incomeDetailService;
	}

	@Resource
	public void setIncomeDetailService(IIncomeDetailService incomeDetailService) {
		this.incomeDetailService = incomeDetailService;
	}
    
	private ICustomerService customerService;
	public ICustomerService getCustomerService() {
		return customerService;
	}
	@Resource
	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}

	public IIncomeInfoService getIncomeInfoService2() {
		return incomeInfoService2;
	}

	@Resource
	public void setIncomeInfoService2(IIncomeInfoService incomeInfoService2) {
		this.incomeInfoService2 = incomeInfoService2;
	}

	public IGoldenBeanDetailService getGoldenBeanDetailService() {
		return goldenBeanDetailService;
	}

	@Resource
	public void setGoldenBeanDetailService(
			IGoldenBeanDetailService goldenBeanDetailService) {
		this.goldenBeanDetailService = goldenBeanDetailService;
	}

	public IGoldenBeanInfoService getIncomeInfoService() {
		return incomeInfoService;
	}

	@Resource
	public void setIncomeInfoService(IGoldenBeanInfoService incomeInfoService) {
		this.incomeInfoService = incomeInfoService;
	}

	public IAccountDetailService getAccountDetailService() {
		return accountDetailService;
	}

	@Resource
	public void setAccountDetailService(
			IAccountDetailService accountDetailService) {
		this.accountDetailService = accountDetailService;
	}

	public IWithdrawApplyforService getWithdrawApplyforService() {
		return withdrawApplyforService;
	}

	@Resource
	public void setWithdrawApplyforService(
			IWithdrawApplyforService withdrawApplyforService) {
		this.withdrawApplyforService = withdrawApplyforService;
	}

	public IAccountInfoService getAccountInfoService() {
		return accountInfoService;
	}

	@Resource
	public void setAccountInfoService(IAccountInfoService accountInfoService) {
		this.accountInfoService = accountInfoService;
	}

	public IAccountRechargeService getAccountRechargeService() {
		return accountRechargeService;
	}

	@Resource
	public void setAccountRechargeService(
			IAccountRechargeService accountRechargeService) {
		this.accountRechargeService = accountRechargeService;
	}

	public WechatPayService getWechatPayService() {
		return wechatPayService;
	}

	@Resource
	public void setWechatPayService(WechatPayService wechatPayService) {
		this.wechatPayService = wechatPayService;
	}

	public IParamOptionDao getParamOptionDao() {
		return paramOptionDao;
	}

	@Resource
	public void setParamOptionDao(IParamOptionDao paramOptionDao) {
		this.paramOptionDao = paramOptionDao;
	}

	public IWithdrawParamService getWithdrawParamService() {
		return withdrawParamService;
	}

	@Resource
	public void setWithdrawParamService(
			IWithdrawParamService withdrawParamService) {
		this.withdrawParamService = withdrawParamService;
	}

	private final String myWealthViewName = "myWealth";
	//金豆相关页面
	private final String goldBeanViewName = "goldBean";
	private final String bonusDetailViewName = "bonusDetail";	
	private final String bonusDetailListViewName = "bonusDetailList";
	private final String bonusViewName = "bonus";
	private final String pushBillGoldBeanListViewName = "pushBillGoldBean";
	private final String pushBillBonusListViewNameString = "pushBillBonusList";
	private final String aboutGoldbeanViewName = "aboutGoldbean";
	private final String goldbeanToBalanceViewName = "goldbeanToBalance";
	private final String doGoldbeanToBalanceViewName = "doGoldbeanToBalance";
	
	//奖金相关页面
	private final String incomeViewName = "income";
	private final String incomeDetailViewName = "incomeDetail";	
	private final String incomeInfoDetailViewName = "incomeInfoDetail";
	private final String incomeDetailListViewName = "incomeDetailList";
	private final String aboutIncomeViewName = "aboutIncome";	
	private final String doIncomeToBalanceViewName = "doIncomeToBalance";
	private final String incomeToBalanceViewName = "incomeToBalance";
	

	//账户余额相关页面	
	private final String balanceViewName = "balance";	
	private final String withdrawViewName = "withdraw";
	private final String rechargeViewName = "recharge";	
	private final String balanceDetailViewName = "balanceDetail";	
	private final String balanceDetailListViewName = "balanceDetailList";
	private final String rechargeResultViewName = "rechargeResult";
	private final String payInfoViewName = "payInfo";	
	private final String submitApplicationViewName = "submitApplication";
	private final String toConfirmViewName = "toConfirm";
	private final String resultDetailViewName = "resultDetail";

	//确认支付密码
	private final String toConfirmPasswordViewName = "toConfirmPassword";
	
	public final static String StrOrderNum = "yyyyMMddHHmmss";

	@ResponseBody
	@RequestMapping(value = toConfirmPasswordViewName, method = {RequestMethod.GET, RequestMethod.POST }, produces = "text/plain;charset=utf-8")
	public String toConfirmPassword(Model model, String customerId,String password) {

		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel);
		Customer customer =  customerService.queryModel(loginModel.getUserId());
		/*Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);*/
		/*System.err.println("原始++++"+password);*/
		String customerPassword=customer.getPassword();
		/*String customerPassword="76e20fd9cbbab91f78c9f77fd73122026eea92d9eb1ec3cc3f96dbc2c51dc86086f5ecb113f02354";*/
		/*System.err.println("1+++++++++"+customerPassword);*/
		String custPassword=null;
		if(!StringUtils.isEmpty(customerPassword)){
			//查出数据库的密码，解密为md5加密的结果(32位)
			custPassword=DESedeCoder.decrypt(customerPassword);
		}
		/*System.err.println("2++++++++"+custPassword);*/
		//将输入密码加密为md5加密的结果(32位)
		String custPass=MD5Utils.encoderByMd5With32Bit(password);
		/*System.err.println("3+++++++++"+custPass);*/
		String result=null;
		if(custPass.equals(custPassword)){
			result = "true";
		}else{
			result = "false";
		}
		/*System.err.println("4++++++++"+result);*/
		return String.valueOf(result);
	}
	
	@RequestMapping(value = rechargeViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String rechaerg(Model model) {
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel);
		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);
		model.addAttribute("customer", customer);

		AccountInfo accountInfo = accountInfoService.queryModelByCustomerId(customer.getId());
		model.addAttribute("accountInfo", accountInfo);
		return view(rechargeViewName);
	}

	@RequestMapping(value = withdrawViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String withdraw(Model model) {
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);

		AccountInfo accountInfo = accountInfoService.queryModelByCustomerId(customer.getId());

		// 提现须知
		WithdrawParam minLimit = withdrawParamService.queryModelByHelpCode("minLimit");
		WithdrawParam perLimit = withdrawParamService.queryModelByHelpCode("perLimit");
		WithdrawParam withdrawCount = withdrawParamService.queryModelByHelpCode("withdrawCount");
		WithdrawParam totalMoney = withdrawParamService.queryModelByHelpCode("totalMoney");
		WithdrawParam minMoney = withdrawParamService.queryModelByHelpCode("minMoney");

		model.addAttribute("customer", customer);
		model.addAttribute("loginModel", loginModel);
		model.addAttribute("accountInfo", accountInfo);

		model.addAttribute("minLimit", minLimit);
		model.addAttribute("perLimit", perLimit);
		model.addAttribute("withdrawCount", withdrawCount);
		model.addAttribute("totalMoney", totalMoney);
		model.addAttribute("minMoney", minMoney);
		return view(withdrawViewName);
	}

	@ResponseBody
	@RequestMapping(value = payInfoViewName, method = { RequestMethod.POST }, produces = "text/plain;charset=utf-8")
	public String getPayInfoJson(HttpServletRequest req, String openId,double money) {
		/* 必填字段 [body, outTradeNo, totalFee, spbillCreateIp] 必须提供值 */
		WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
		request.setOutTradeNo("" + System.currentTimeMillis()
				+ RandomUtils.randomInt(1000, 9999));
		request.setTotalFee(WxPayBaseRequest.yuanToFee(String.valueOf(money)));
		request.setSpbillCreateIp("123.207.34.17");
		request.setBody("充值");
		request.setOpenid(openId);

		Map<String, String> payInfoMap = null;
		try {
			payInfoMap = wechatPayService.getPayInfo(request);

			payInfoMap.put("outTradeNo", request.getOutTradeNo());
			/* payInfoMap.put("paymentNo", value) */
		} catch (WxPayException e) {
			e.printStackTrace();
		}
		payInfoMap.get("package").replace("prepay_id=", "");

		return JsonUtils.toJson(payInfoMap);

	}

	@ResponseBody
	@RequestMapping(value = rechargeResultViewName, method = {RequestMethod.GET, RequestMethod.POST }, produces = "text/plain;charset=utf-8")
	public String toRecharge(Model model, Double money, String customerId,String outTradeNo) {

		accountRechargeService.updateByRecharge(customerId, money, outTradeNo);
		return String.valueOf(true);
	}

	// 提现操作
	@ResponseBody
	@RequestMapping(value = submitApplicationViewName, method = {RequestMethod.GET, RequestMethod.POST }, produces = "text/plain;charset=utf-8")
	public String submitApplication(Model model, Double money) {

		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel);
		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);
		model.addAttribute("customer", customer);

		System.err.println("钱到账么了\t" + money);
		HashMap<String, String> map = null;
		map = withdrawApplyforService.applyLimit(customer, money);

		return JsonUtils.toJson(map);
	}

	@RequestMapping(value = myWealthViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String myWhealth(Model model) {

		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel);

		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);
		model.addAttribute("customer", customer);

		AccountInfo accountInfo = new AccountInfo();
		accountInfo = accountInfoService.queryModelByCustomerId(customer.getId());
		model.addAttribute("accountInfo", accountInfo);

		GoldenBeanInfo goldenBeanInfo = new GoldenBeanInfo();
		goldenBeanInfo = incomeInfoService.queryModelByCustomerId(customer.getId());
		model.addAttribute("goldenBeanInfo", goldenBeanInfo);

		IncomeInfo incomeInfo = new IncomeInfo();
		incomeInfo = incomeInfoService2.queryIncomeInfo(customer.getId());
		model.addAttribute("incomeInfo", incomeInfo);

		return view(myWealthViewName);
	}

	@RequestMapping(value = incomeDetailViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String incomeDetail(Model model) {
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel);

		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);
		model.addAttribute("customer", customer);

		return view(incomeDetailViewName);
	}

	// 按条件查询奖金明细
	@RequestMapping(value = incomeDetailListViewName, method = {RequestMethod.GET, RequestMethod.POST })
	public String incomeDetailList(Model model,FindIncomeDetail findIncomeDetail) {
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel);

		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);
		model.addAttribute("customer", customer);

		List<IncomeDetail> incomeDetails = incomeDetailService.queryIncomeDetail(findIncomeDetail, customer.getId());
		model.addAttribute("incomeDetails", incomeDetails);

		Double incomeMoney = null;
		IncomeDetail incomeDetail1 = incomeDetailService.querySumIncomeMoney(findIncomeDetail, customer.getId());
		if (incomeDetail1 == null) {
			incomeMoney = 0.00;
		} else {
			incomeMoney = incomeDetail1.getIncomeMoney();
		}
		model.addAttribute("incomeMoney", incomeMoney);

		Double expendMoney = null;
		IncomeDetail incomeDetail2 = incomeDetailService.querySumExpendMoney(findIncomeDetail, customer.getId());
		if (incomeDetail2 == null) {
			expendMoney = 0.00;
		} else {
			expendMoney = incomeDetail2.getExpendMoney();
		}
		model.addAttribute("expendMoney", expendMoney);

		return view(incomeDetailListViewName);
	}

	/*--------------奖金明细---------------*/
	@RequestMapping(value = incomeInfoDetailViewName, method = {RequestMethod.GET, RequestMethod.POST })
	public String incomeInfoDetail(Model model) {
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel);
		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);
		model.addAttribute("customer", customer);

		List<IncomeDetail> incomeDetails = incomeDetailService.queryIncomeDetailInfo(customer.getId());
		model.addAttribute("incomeDetails", incomeDetails);

		return view(incomeInfoDetailViewName);
	}

	// 按条件查询金豆明细
	@RequestMapping(value = bonusDetailListViewName, method = {RequestMethod.GET, RequestMethod.POST })
	public String bonusDetailList(Model model, FindGoldenDetail findGoldenDetail) {
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel);

		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);
		model.addAttribute("customer", customer);

		List<GoldenBeanDetail> goldenBeanDetails = goldenBeanDetailService.queryGoldenBeanDetail(findGoldenDetail, customer.getId());
		model.addAttribute("goldenBeanDetails", goldenBeanDetails);

		Double incomeMoney = null;
		GoldenBeanDetail goldenBeanDetail1 = goldenBeanDetailService.querySumIncomeMoney(findGoldenDetail, customer.getId());
		if (goldenBeanDetail1 == null) {
			incomeMoney = 0.00;
		} else {
			incomeMoney = goldenBeanDetail1.getIncomeMoney();
		}
		model.addAttribute("incomeMoney", incomeMoney);

		Double expendMoney = null;
		GoldenBeanDetail goldenBeanDetail2 = goldenBeanDetailService.querySumExpendMoney(findGoldenDetail, customer.getId());
		if (goldenBeanDetail2 == null) {
			expendMoney = 0.00;
		} else {
			expendMoney = goldenBeanDetail2.getExpendMoney();
		}
		model.addAttribute("expendMoney", expendMoney);

		return view(bonusDetailListViewName);
	}

	/*--------------奖金详情--------------*/
	@RequestMapping(value = incomeViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String income(Model model, String id) {

		IncomeDetail incomeDetail = incomeDetailService.queryIncomeDetail(id);
		model.addAttribute("incomeDetail", incomeDetail);

		return view(incomeViewName);
	}

	/*--------------余额明细--------------*/
	@RequestMapping(value = balanceDetailViewName, method = {
			RequestMethod.GET, RequestMethod.POST })
	public String balanceDetail(Model model) {
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel);
		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);
		model.addAttribute("customer", customer);

		List<AccountDetail> accountDetails = accountDetailService.queryBalanceList(customer.getId());
		model.addAttribute("accountDetails", accountDetails);

		return view(balanceDetailViewName);
	}

	// 按条件查询余额明细
	@RequestMapping(value = balanceDetailListViewName, method = {RequestMethod.GET, RequestMethod.POST })
	public String balanceDetailList(Model model,FindAccountDetail findAccountDetail) {
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel);

		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);
		model.addAttribute("customer", customer);

		List<AccountDetail> accountDetails = accountDetailService.queryAccountDetail(findAccountDetail, customer.getId());
		model.addAttribute("accountDetails", accountDetails);

		Double incomeMoney = null;
		AccountDetail accountDetail1 = accountDetailService.querySumIncomeMoney(findAccountDetail, customer.getId());
		if (accountDetail1 == null) {
			incomeMoney = 0.00;
		} else {
			incomeMoney = accountDetail1.getIncomeMoney();
		}
		model.addAttribute("incomeMoney", incomeMoney);

		Double expendMoney = null;
		AccountDetail accountDetail2 = accountDetailService.querySumExpendMoney(findAccountDetail, customer.getId());
		if (accountDetail2 == null) {
			expendMoney = 0.00;
		} else {
			expendMoney = accountDetail2.getExpendMoney();
		}
		model.addAttribute("expendMoney", expendMoney);

		return view(balanceDetailListViewName);
	}

	/*--------------余额详情--------------*/
	@RequestMapping(value = balanceViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String balance(Model model, String id) {

		AccountDetail accountDetail = accountDetailService.queryAccount(id);
		model.addAttribute("accountDetail", accountDetail);

		return view(balanceViewName);
	}

	@RequestMapping(value = goldBeanViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String goldBean(Model model) {
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel);
		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);

		model.addAttribute("customer", customer);
		AccountInfo accountInfo = new AccountInfo();

		accountInfo = accountInfoService.queryModelByCustomerId(customer.getId());
		model.addAttribute("accountInfo", accountInfo);

		return view(goldBeanViewName);
	}

	/*--------------金豆明细--------------*/
	@RequestMapping(value = bonusDetailViewName, method = { RequestMethod.GET,
			RequestMethod.POST })
	public String bonusDetail(Model model, GoldenBeanDetailSearch search,FindGoldenDetail findGoldenDetail) {
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel);

		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);
		model.addAttribute("customer", customer);

		List<GoldenBeanDetail> goldenBeanDetails = goldenBeanDetailService
				.queryBonusList(search, customer.getId());
		model.addAttribute("goldenBeanDetails", goldenBeanDetails);

		GoldenBeanDetail goldenBeanDetail1 = goldenBeanDetailService.querySumIncomeMoney(findGoldenDetail, customer.getId());
		model.addAttribute("goldenBeanDetail1", goldenBeanDetail1);

		GoldenBeanDetail goldenBeanDetail2 = goldenBeanDetailService.querySumExpendMoney(findGoldenDetail, customer.getId());
		model.addAttribute("goldenBeanDetail2", goldenBeanDetail2);

		return view(bonusDetailViewName);
	}

	/*--------------金豆详情--------------*/
	@RequestMapping(value = bonusViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String bonus(Model model, String id) {

		GoldenBeanDetail goldenBeanDetail = goldenBeanDetailService.queryGoldenBean(id);
		model.addAttribute("goldenBeanDetail", goldenBeanDetail);

		return view(bonusViewName);
	}

	@RequestMapping(value = aboutGoldbeanViewName, method = {RequestMethod.GET, RequestMethod.POST })
	public String aboutGoldbean(Model model) {

		// 得到 金豆 对 余额的 比例
		ParamOption paramOption = paramOptionDao.queryModelByParamName("exchange");
		int exchangeRate = Integer.valueOf(paramOption.getParamValue());

		HashMap<String, Object> res = queryUserBean();

		model.addAttribute("uselessgolden", res.get("uselessgolden"));
		model.addAttribute("usegolden", res.get("usegolden"));
		model.addAttribute("allgolden", res.get("allgolden"));
		model.addAttribute("exchangeRate", exchangeRate);
		return view(aboutGoldbeanViewName);
	}
	
	@RequestMapping(value = aboutIncomeViewName, method = { RequestMethod.GET,
			RequestMethod.POST })
	public String aboutIncome(Model model) {
		
		HashMap<String, Object> res = queryUserIncome();
		
		model.addAttribute("uselessIncome", res.get("uselessIncome"));
		model.addAttribute("useIncome", res.get("useIncome"));
		model.addAttribute("allIncome", res.get("allIncome"));
		return view(aboutIncomeViewName);
	}
	
	@RequestMapping(value = doGoldbeanToBalanceViewName, method = {RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public String doGoldbeanToBalance(Model model, String money) {

		HashMap<String, Object> val = queryUserBean();
		
		double userGoldBean = (double)val.get("usegolden");
		double changeMoney = Double.valueOf(money);
		if(changeMoney>0&&changeMoney<=userGoldBean){
			
			WechatLoginModel loginModel = wechatSession.getCurrentLogin();
			Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);

			Boolean res = accountInfoService.updateGoldToBalance(customer,
					Double.valueOf(money));

			if (res) {
				return "true";
			} else {
				return "false";
			}
		}else {
			return "false";
		}		
	}

	@RequestMapping(value = doIncomeToBalanceViewName, method = {RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public String doIncomeToBalance(Model model, String money) {

		HashMap<String, Object> val = queryUserIncome();
		
		double useIncome =(double)val.get("useIncome");
		double changeIncome = Double.valueOf(money);
		if(changeIncome>0&&changeIncome<=useIncome){
			
			WechatLoginModel loginModel = wechatSession.getCurrentLogin();
			Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);

			Boolean res = accountInfoService.updateIncomeToBalance(customer,Double.valueOf(money));

			if (res) {
				return "true";
			} else {
				return "false";
			}
		}else {
			return "false";
		}
		

	}

	//提取可用金豆 不可用金豆 代码
	public HashMap<String, Object> queryUserBean(){
		//得到 当前用户余额
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);

		AccountInfo accountInfo = new AccountInfo();
		accountInfo = accountInfoService.queryModelByCustomerId(customer.getId());

		Double allgolden = accountInfo.getGoldenBean();

		// 得到犹豫期余额
		Double uselessgolden = 0.0;
		GoldenBeanDetail goldenBeanDetail = goldenBeanDetailService.queryGoldenInHesitate(customer.getId());
		if (goldenBeanDetail == null) {
			uselessgolden = 0.0;
		} else {
			uselessgolden = goldenBeanDetail.getSumgolden();
		}
		Double usegolden = allgolden - uselessgolden;
		
		HashMap<String, Object> res = new HashMap<String, Object>();
		res.put("allgolden", allgolden);
		res.put("usegolden", usegolden);
		res.put("uselessgolden", uselessgolden);
		return res;
	}
	
	//提取可用奖金 不可用奖金  代码
	public HashMap<String, Object> queryUserIncome(){
		
		// 等到 当前用户余额
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);

		IncomeInfo incomeInfo = new IncomeInfo();
		incomeInfo = incomeInfoService2.queryIncomeInfo(customer.getId());

		Double allIncome = incomeInfo.getIncomeBalance();

		// 得到犹豫期余额
		Double uselessIncome = 0.0;
		IncomeDetail incomeDetail = incomeDetailService.queryIncomeInHesitate(customer.getId());
		if (incomeDetail == null) {
			uselessIncome = 0.0;
		} else {
			uselessIncome = incomeDetail.getSumincome();
		}
		Double useIncome = allIncome - uselessIncome;
		
		HashMap<String, Object> res = new HashMap<String, Object>();
		res.put("allIncome", allIncome);
		res.put("useIncome", useIncome);
		res.put("uselessIncome", uselessIncome);
		return res;
	}
	
	/***************************************** 发送奖金信息 ********************************************/

	@RequestMapping(value = pushBillGoldBeanListViewName, method = {
			RequestMethod.GET, RequestMethod.POST })
	public String pushBillGoldBeanList(Model model, String custId,String endTime) {
		List<GoldenBeanDetail> goldenBeanDetails = null;

		if (!StringUtils.isEmpty(endTime)) {
			String str = endTime;
			Long timeLong = Long.parseLong(str.trim());
			String dateString = DateUtils.formatDateTime(timeLong);

			FindGoldenDetail findGoldenDetail = new FindGoldenDetail();
			findGoldenDetail.setEndDate(DateUtils.parseDateTime(dateString));
			findGoldenDetail.setStartDate(DateUtils.addDay(-1,DateUtils.parseDateTime(dateString)));

			goldenBeanDetails = goldenBeanDetailService.queryGoldenBeanDetail(
					findGoldenDetail, custId);
		}

		model.addAttribute("goldenBeanDetails", goldenBeanDetails);
		return view(pushBillGoldBeanListViewName);
	}

	@RequestMapping(value = pushBillBonusListViewNameString, method = {
			RequestMethod.GET, RequestMethod.POST })
	public String pushBillBonusList(Model model, String custId, String endTime) {
		List<IncomeDetail> incomeDetails = null;
		if (!StringUtils.isEmpty(endTime)) {
			String str = endTime;
			Long timeLong = Long.parseLong(str.trim());
			String dateString = DateUtils.formatDateTime(timeLong);

			FindIncomeDetail findIncomeDetail = new FindIncomeDetail();
			findIncomeDetail.setEndDate(DateUtils.parseDateTime(dateString));
			findIncomeDetail.setStartDate(DateUtils.addDay(-1,DateUtils.parseDateTime(dateString)));

			incomeDetails = incomeDetailService.queryIncomeDetail(
					findIncomeDetail, custId);
		}
		model.addAttribute("incomeDetails", incomeDetails);
		return view(pushBillBonusListViewNameString);
	}

	// 免审核提现
	@RequestMapping(value = toConfirmViewName, method = { RequestMethod.POST }, produces = "text/plain;charset=utf-8")
	@ResponseBody
	public String toConfirm(Model model, String key, HttpServletRequest req)throws Exception {
		System.err.println("传过来的主键\t" + key);

		Withdraw withdraw = withdrawApplyforService.updateToConfirm(key);
		if (withdraw != null) {
			WxEntPayRequest request = new WxEntPayRequest();
			request.setOpenid(withdraw.getOpenId());
			request.setCheckName("NO_CHECK");
			request.setNonceStr("" + System.currentTimeMillis()+ RandomUtils.randomInt(1000, 9999));
			request.setPartnerTradeNo("" + System.currentTimeMillis()+ RandomUtils.randomInt(1000, 9999));
			request.setAmount(withdraw.getAmount());
			request.setSpbillCreateIp("123.207.34.17");
			request.setDescription("提现");

			WxEntPayResult result = null;
			try {
				result = wechatPayService.entPay(request);
			} catch (WxPayException e) {
				e.printStackTrace();
			}
			withdraw.setOutTradeNo(result.getPartnerTradeNo());

			// 根据回调结果做出相应操作
			if (result != null && "SUCCESS".equals(result.getResultCode())&& "SUCCESS".equals(result.getReturnCode())) {

				withdraw.setPaymentNo(result.getPaymentNo());
				withdraw.setComment("提现到账成功");
				if (withdrawApplyforService.updateGoldBeanNum(withdraw, null)) {

					return JsonUtils.toJson("success");
				}
			}

			withdraw.setComment("提现到账失败");
			withdrawApplyforService.updateForRollBack(withdraw, null);

			return JsonUtils.toJson("error");
		}

		return JsonUtils.toJson("error");
	}

	@RequestMapping(value = resultDetailViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String resultDetail(Model model, String changeMoney) {

		model.addAttribute("changeMoney", changeMoney);
		return view(resultDetailViewName);
	}
	
	@RequestMapping(value = goldbeanToBalanceViewName, method = {RequestMethod.GET, RequestMethod.POST }, produces = "text/plain;charset=utf-8")
	public String goldbeanToBalance(Model model) {
		// 得到 金豆 对 余额的 比例
				ParamOption paramOption = paramOptionDao.queryModelByParamName("exchange");
				int exchangeRate = Integer.valueOf(paramOption.getParamValue());

				HashMap<String, Object> res = queryUserBean();
				
				model.addAttribute("allgolden", res.get("allgolden"));
				model.addAttribute("uselessgolden", res.get("uselessgolden"));
				model.addAttribute("usegolden", res.get("usegolden"));
				model.addAttribute("exchangeRate", exchangeRate);
		return view(goldbeanToBalanceViewName);
	}
	
	@RequestMapping(value = incomeToBalanceViewName, method = {RequestMethod.GET, RequestMethod.POST }, produces = "text/plain;charset=utf-8")
	public String incomeToBalance(Model model) {
		HashMap<String, Object> res = queryUserIncome();
		
		model.addAttribute("uselessIncome", res.get("uselessIncome"));
		model.addAttribute("useIncome", res.get("useIncome"));
		model.addAttribute("allIncome", res.get("allIncome"));
		return view(incomeToBalanceViewName);
	}
}
